package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.adminDto.AdChannelDto;
import com.heima.model.admin.adminDto.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 石头
 * @description 针对表【wm_channel(频道信息表)】的数据库操作Service实现
 * @createDate 2022-06-14 18:27:38
 */
@Service
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel>
        implements WmChannelService {

    @Resource
    private WmNewsService wmNewsService;

    /**
     * 查询所有频道
     *
     * @return
     */
    @Override
    public ResponseResult findAllChannels() {
        log.info("查询所有频道");
        List<WmChannel> list = this.list(Wrappers.<WmChannel>lambdaQuery().orderByDesc(WmChannel::getOrd).eq(WmChannel::getStatus, 1));
        return ResponseResult.okResult(list);
    }

    /**
     * 频道分页查询
     *
     * @param dto
     * @return
     * @deprecated 查询 wm_channel 表  条件：频道名称模糊查询、账号状态 true or false 、时间倒序分页
     */
    @Override
    public ResponseResult findChannelList(ChannelDto dto) {
        log.info("频道分页查询已经执行");
        // 参数校验
        Integer size = dto.getSize();
        Integer page = dto.getPage();
        String name = dto.getName();
        if (size == null || page == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 构造分页器
        IPage<WmChannel> channelPage = new Page<>(page, size);
        // 构造查询条件
        LambdaQueryWrapper<WmChannel> wrapper = Wrappers.<WmChannel>lambdaQuery().orderByDesc(WmChannel::getCreatedTime);
        //关键字模糊查询
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(WmChannel::getName, name);
        }
        // 查询
        IPage<WmChannel> wmChannelIPage = page(channelPage, wrapper);
        ResponseResult pageResponseResult = new PageResponseResult((int) wmChannelIPage.getCurrent(), (int) wmChannelIPage.getPages(), (int) wmChannelIPage.getTotal());
        pageResponseResult.setData(wmChannelIPage.getRecords());
        return pageResponseResult;
    }

    /**
     * 频道修改
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateChannel(AdChannelDto dto) {
        log.info("频道修改已经执行");
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer id = dto.getId();
        if (id == null || id < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 查询当前频道id是否有引用文章
        LambdaQueryWrapper<WmNews> queryWrapper = Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, id);
        queryWrapper.last("limit 1");
        WmNews wmNews = wmNewsService.getOne(queryWrapper);
        // 有引用不能禁用频道
        if (wmNews != null && !dto.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ARTICLE_QUOTED);
        }
        // 修改频道
        WmChannel wmChannel = this.getById(id);
        BeanUtils.copyProperties(dto, wmChannel);
        if (dto.getStatus()) {
            wmChannel.setStatus(1);
        } else {
            wmChannel.setStatus(0);
        }
        boolean updateFlag = this.updateById(wmChannel);

        return updateFlag ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.ERROR);

    }

    /**
     * 频道新增
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveChannel(AdChannelDto dto) {
        log.info("频道新增已经执行");
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        String name = dto.getName();
        if (!StringUtils.isNotBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 新增频道信息，先查询频道是否已经存在，不存在新增
        LambdaQueryWrapper<WmChannel> queryWrapper = Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, name);
        WmChannel channel = this.getOne(queryWrapper);
        if (channel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ERROR_CHANNEL);
        }
        // 属性拷贝新增
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(dto, wmChannel);
        if (dto.getStatus()) {
            wmChannel.setStatus(1);
        } else {
            wmChannel.setStatus(0);
        }
        boolean saveFlag = this.save(wmChannel);

        return saveFlag ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.ERROR);
    }

    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delChannel(Integer id) {
        log.info("删除频道已经执行");
        if (id == null || id < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 频道为禁止状态才能删除
        LambdaQueryWrapper<WmChannel> wrapper = Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getId, id);
        WmChannel wmChannel = this.getOne(wrapper);
        if (wmChannel.getStatus() != 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ERROR_CHANNEL_FORBID);
        }

        boolean removeFlag = this.removeById(id);

        return removeFlag ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.ERROR);
    }


}




